<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use EasyWeChat\Foundation\Application;
use think\Config;
 /** 用户微信提现管理
  *
  * @icon fa fa-circle-o
  */
class Deposit extends Backend
{
    
    /**
     * Deposit模型对象
     * @var \app\admin\model\Deposit
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Deposit;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','weChatCcount','wechantNickname','iphone','cash_fee','money','status','moneylog_id','reviewuser_id','reviewtime','remark','createtime']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detail($ids){
        $this->view->assign('id',$ids);
        return $this->view->fetch();
    }

    public function up(){
        if ($this->request->isAjax()) {
            $id=$this->request->post('id');
            $status=$this->request->post('status');
            $reviewuser_id=$this->auth->id;
            $posit=$this->model->get(['id' => $id]);
            if($posit['status']==2){
                $this->error('已经审核过了');
                exit;
            }
            $suc=$this->model->upDeposit($id,$status,$reviewuser_id);
            if($suc>0){
                $user=Db::name('user')->where(['id'=>$posit['user_id']])->find();
                if($status==2){
                    try{
//                        $options =configNew();
//
//                        $app = new Application($options);
//
//                        $merchantPay = $app->merchant_pay;
//                        $merchantPayData = [
//                            'partner_trade_no' =>$posit['partner_trade_no'], //随机字符串作为订单号，跟红包和支付一个概念。
//                            'openid' => $posit['openid'], //收款人的openid
//                            'check_name' => 'NO_CHECK',  //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
//                            're_user_name'=>'',     //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
//                            'amount' => (float)$posit['money']*100,  //单位为分
//                            'desc' => '企业付款',
//                            'spbill_create_ip' => request()->ip(),  //发起交易的IP地址
//                        ];
//
//                        $result = $merchantPay->send($merchantPayData);
//                        $this->error(json($merchantPayData));
//                        if($result['return_code']=='SUCCESS'&&$result['result_code']=='SUCCESS'){
//                            $positData['status']=4;
//                        }else{
                            $positData['status']=2;
//                        }
                        $ti_fee=$posit['money']+$posit['cash_fee'];
                        $data['user_id']=$posit['user_id'];
                        $data['originUserId']=0;
                        $data['money']=$ti_fee;
                        $data['before']=(float)$user['money'];
                        $data['after']=(float)$user['money']-$ti_fee;
                        $data['note']='提现';
                        $data['type']=3;
                        $data['something_id']=0;
                        $data['createtime']=time();
                        $moneyLogId=Db::name('user_money_log')->insertGetId($data);
                        $user_status=Db::name('user')->where(['id'=>$posit['user_id']])->setDec('money',$ti_fee);
                        $positData['moneylog_id']=$moneyLogId;
                        $positStatus=Db::name('user_deposit')->where(['id'=>array('eq',$id)])->update($positData);
                    }catch (\Exception $e){
                        $this->error($e->getMessage());
                    }



                    $this->success("审核成功", null, $suc);
                }elseif($status==3){
//                    $ti_fee=$posit['money']+$posit['cash_fee'];
//                    Db::name('user')->where(['id'=>$posit['user_id']])->setInc('money',$ti_fee);

//                    if(!empty($posit['moneylog_id'])){
//                        Db::name('user_money_log')->where(['id'=>$posit['moneylog_id']])->update();
//                    }

                    $this->success("提交成功", null, $suc);
                }

            }else{
                $this->success("审核失败", null, $suc);
            }

        }
    }
}
